
var kendoChartUI = function(object,chartJson,type){
	if(isEmpty(object) || isEmpty(chartJson) || isEmpty(type)) return;
	var kendoChart = null;
	if(type == "DATE"){
		kendoChart = new scatterKendoChart(object,chartJson);
	}else if(type == "NUMBER"){
		kendoChart =  new sysLineChart(object,chartJson);
	}else if(type == "GAUGE"){
		kendoChart =  new sysGauge(object,chartJson);
	}else if(type == "COLUMN"){
		kendoChart =  new sysColumnChart(object,chartJson);
	}else if(type == "DRAGCOLUMN"){
		kendoChart =  new sysDRAGColumnChart(object,chartJson);
	}
	kendoChart.type = type;
	return kendoChart;
};



//时间刻度线性报表
var scatterKendoChart = function(object,chartJson){
	this.chartJson = jQuery.extend({},chartJson);
	this.object = object;
	this.kendoChart;
	this.init(object,this.chartJson);
	return this;
};

scatterKendoChart.prototype={
		init:function(object,chartJson){//生成监控图表
			if(isEmpty(object) || isEmpty(chartJson)) return;
			var minDate=chartJson.minDate || new Date();
			var maxDate=chartJson.maxDate || new Date();
			if(isEmpty(chartJson.maxDate)){
				maxDate = new Date(maxDate.setMinutes(maxDate.getMinutes()+30));
			}
			if(chartJson.auto){
				minDate = new Date("2000/01/01 00:00:00");
				maxDate = new Date("2000/01/01 00:00:01");
			}
			var position = chartJson.position || "top";
			//var series =  jQuery.extend([{}], chartJson.series);
			var  baseUnit=chartJson.baseUnit || "minutes";
			var minutes = chartJson.minutes || "HH:mm";
			var Step = chartJson.chartStep  || 5;
			var majorUnit = chartJson.majorUnit  || 1;
			var xFormat = chartJson.xFormat	|| "HH:mm";
			var yFormat = chartJson.yFormat	|| "{0}";
			var yMax = chartJson.yMax || 1;
			var format = chartJson.format;
			if(isEmpty(format)){
				format="{1} {0:yyyy-MM-dd HH:mm:ss}";
			}
//			var titleFont = chartJson.titleFont;
			var titleColor = chartJson.titleColor;
			var legendFont = chartJson.legendFont;
			var legendColor = chartJson.legendColor;
			var axisFont = chartJson.axisFont;
			var axisColor = chartJson.axisColor;
			var chartArea = chartJson.chartArea;
			this.oldTitle ={text:chartJson.title};
			var title =   isEmpty(chartJson.title)?null:t(chartJson.x18nName+"."+chartJson.title);
			title = isEmpty(title)?chartJson.title:title;
			var visible = isEmpty(chartJson.visible)?true:chartJson.visible ;
			var series =  new Array();
//			if (null == titleFont || undefined == titleFont){
//			titleFont="";
//			}
			if (null == titleColor || undefined == titleColor){
				titleColor="#000000";
			}
			if (null == legendColor || undefined == legendColor){
				legendColor="#000000";
			}
			if (null == axisColor || undefined == axisColor){
				axisColor="#000000";
			}
			if(chartArea==null){
				chartArea={background: ""};
			}
			for(var i = 0;i<chartJson.series.length;i++){
				series.push(jQuery.extend({}, chartJson.series[i]));
				if(isNotEmpt(series[i].name)){
					var name = t(chartJson.x18nName+"."+series[i].name);
					series[i].name = isNotEmpt(name)?name:series[i].name;
				}
				series[i].data = [];
			}
			this.chartJson.height=$(object).height();
			this.chartJson.width=$(object).width();
			this.chartJson.auto =  this.chartJson.auto || false;
			$(object).kendoChart({
				title: {
					text: title,
					color:titleColor
				},
				legend: {
					labels: {
						color:legendColor
					},
					position: position,
					offsetY: 10,
					margin: {
						top: -10
					}	
				},
				seriesDefaults: {
					type: "scatterLine",
					scatterLine: {
						width: 2
					},
					markers: {
						visible: true,
						size: 0
					}
				},
				chartArea:chartArea,
				series:series,
				xAxis: {
					type: "date",
					baseUnit: baseUnit,
					min:minDate,
					max:maxDate,
					labels: {
						format:xFormat,
						font: "10px sans-serif",
						color:axisColor,
						dateFormats: {
							minutes: minutes
						},
						step:Step
					},
					majorUnit: majorUnit
				},
				yAxis: {
					max: yMax,
					labels: {
						format: yFormat,
						color:axisColor,
						font: "10px sans-serif"
					}
				},

				tooltip: {
					visible: visible,
					format: format
				},
				transitions: false
			});
			this.kendoChart = $(object).data("kendoChart");
			this.chartJson.maxDate = maxDate;
			this.chartJson.minDate = minDate;
			this.fillChartData(chartJson.series);
		},
		fillChartData:function(dataJson){   //填充数据
			if(isEmpty(dataJson)) return;
			var maxDate  = this.kendoChart.options.xAxis.max;
			var minDate  = this.kendoChart.options.xAxis.min;
			var seriesArray = this.kendoChart.options.series;
			var intervalTime = maxDate.getTime() - minDate.getTime(); 
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			//把新的数据加载到指定的线性报表中
			if(dataJson.constructor == Object){
				for(var names in dataJson){
					var seriesData = serMap.get(names);
					var arrayDate = this.chartJson.auto?dataJson[names]:seriesData.data.concat(dataJson[names]);
					seriesData.data = arrayDate;
					serMap.remove(names);
					serMap.put(names, seriesData);
				}
			}else if(dataJson.constructor == Array){
				for(var i = 0;i<dataJson.length;i++){
					var seriesData = serMap.get(dataJson[i].cname);
					var arrayDate = this.chartJson.auto?dataJson[i].data:seriesData.data.concat(dataJson[i].data);
					seriesData.data = arrayDate;
					serMap.remove(dataJson[i].cname);
					serMap.put(dataJson[i].cname, seriesData);
				}
			}
			var seriesMaxDate = null;
			var seriesMinDate = null;
			seriesArray = serMap.values();
			this.ymaxNumber = this.kendoChart.options.yAxis.max; //Y轴最大值
			for(var i =0;i<seriesArray.length;i++){ //对新报表数据进行时间排序
				var data = this.sortDataTime(seriesArray[i].data);
				if(isEmpty(data)) return;
				seriesArray[i].data = data;
				seriesMaxDate = data[data.length - 1][0];
				seriesMinDate = data[0][0];
			}
			//判断最大时间是否为空,并且是否超出报表的最大时间
			if(isNotEmpt(seriesMaxDate) && seriesMaxDate.getTime() > maxDate.getTime()){ 
				maxDate.setTime(seriesMaxDate.getTime());
				if(this.chartJson.auto){
					minDate.setTime(seriesMinDate);
				}else{
					minDate.setTime(seriesMaxDate.getTime() - intervalTime);
				}
			}
			//不是根据时间自适应并且数据最大时间比报表最大时间相隔超过30分钟
			if(!this.chartJson.auto && 
					seriesMaxDate.getTime() + 60*30*1000 < maxDate.getTime()){   
				maxDate.setTime(seriesMaxDate.getTime());
				minDate.setTime(seriesMaxDate.getTime() - intervalTime);
			}
			//如果是自适应
			if(this.chartJson.auto && isNotEmpt(seriesMaxDate)){
				maxDate.setTime(seriesMaxDate.getTime());
				minDate.setTime(seriesMinDate.getTime());
			}
			for(var i =0;i<seriesArray.length;i++){
				seriesArray[i].data = this.removData(seriesArray[i].data, minDate);
			}
			this.kendoChart.options.xAxis.max = maxDate;
			this.kendoChart.options.xAxis.min = minDate;
			var oldMax = this.kendoChart.options.yAxis.max;
			this.kendoChart.options.yAxis.max = oldMax < this.ymaxNumber ? this.ymaxNumber <10?this.ymaxNumber + 1 :this.ymaxNumber*1.1 :oldMax ;
			this.kendoChart.setOptions({series:seriesArray,xAxis:this.kendoChart.options.xAxis,yAxis:this.kendoChart.options.yAxis});
			this.chartJson.series = seriesArray;
			this.chartJson.maxDate = maxDate;
			this.chartJson.minDate = minDate;
			this.kendoChart.refresh();
			if($(this.object).height() <=0){
				$(this.object).height(this.chartJson.height);
				$(this.object).width(this.chartJson.width);
			}

		},
		sortDataTime:function(dataArray){  //对报表数据进行时间排序
			for(var i = 0;i<dataArray.length;i++){   //冒泡排序
				if(this.ymaxNumber < parseFloat(dataArray[i][1])){
					this.ymaxNumber  = parseFloat(dataArray[i][1]);
				}
				for(var j = dataArray.length - 1; j > i; j--){
					if (dataArray[j][0].getTime() < dataArray[j - 1][0].getTime())
					{
						var temp = dataArray[j];
						dataArray[j] = dataArray[j - 1];
						dataArray[j - 1] = temp;
					}
				}
			}
			return dataArray;
		},
		removData:function(dataArray,minDate){  //移除在最小时间之外的报表数据
			for(var i = 0;i<dataArray.length;i++){
				if(dataArray[i][0].getTime() < minDate.getTime()){
					dataArray.splice(i,1);
				}else{
					return dataArray;
				}
			}
			return dataArray;
		},
		//国际化kendoChart报表
		x18nKendoChart:function(){
			var newTitle =  jQuery.extend({}, this.oldTitle);
			var oldseries = this.chartJson.series;
			if(isNotEmpt(newTitle.text)){
				newTitle.text = this.combinationTitle + t(this.chartJson.x18nName +"."+newTitle.text);
			}
			var newseries = this.kendoChart.options.series;
			for(var i = 0 ;i < newseries.length;i++){
				if(isNotEmpt(oldseries[i]["name"])){
					var x18nName = t(this.chartJson.x18nName +"."+oldseries[i]["name"]);
					newseries[i]["name"] = isEmpty(newseries[i]["newNames"]) ? x18nName : newseries[i]["newNames" + x18nName];
				}
			}
			this.kendoChart.setOptions({title:newTitle,series:newseries});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表标题名字
		setTitle:function(titles){
			this.combinationTitle = titles;
			var newTitle =  jQuery.extend({}, this.oldTitle);
			newTitle.text =titles +  t(this.chartJson.x18nName +"."+newTitle.text);
			this.kendoChart.options.title.text = newTitle.text;
			this.kendoChart.setOptions({title:this.kendoChart.options.title});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表数据行名称
		setDataName:function(dataArray){
			if(isEmpty(dataArray)) return;
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			for(var i =0;i<dataArray.length;i++){
				var cname = dataArray[i].cname;
				var seriesData = serMap.get(cname);
				seriesData.name =dataArray[i].newNames + t(this.chartJson.x18nName +"." + dataArray[i].name);
				seriesData.newNames = dataArray[i].newNames;
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
		},
		//把报表数据源置空
		setNullDataSource:function(){
			$.each(this.kendoChart.options.series,function(){
				this.data=[];
			});
			if(this.chartJson.auto){
				this.kendoChart.options.xAxis.max = new Date("2000/01/01 00:00:01");
				this.kendoChart.options.xAxis.min = new Date("2000/01/01 00:00:00");
				this.kendoChart.refresh();
				return;
			}
			var minDate=new Date();
			var maxDate=new Date();
			maxDate = new Date(maxDate.setMinutes(maxDate.getMinutes()+30));
			this.kendoChart.options.xAxis.max = maxDate;
			this.kendoChart.options.xAxis.min = minDate;
			this.chartJson.maxDate = maxDate;
			this.chartJson.minDate = minDate;
			this.kendoChart.refresh();
		}

};



//非时间刻度线性报表
var sysLineChart = function(object,chartJson){
	this.chartJson = jQuery.extend({},chartJson);
	this.object = object;
	this.kendoChart;
	this.maxDot = 50;  //最多保留多少个点
	this.init(object,this.chartJson);
	return this;
};

sysLineChart.prototype={
		init:function(object,chartJson){
			if(isEmpty(object) || isEmpty(chartJson)) return;
			var position = chartJson.position || "top";
			var yMax = chartJson.yMax || 1;
			var yMin = chartJson.yMin || 0;
			var xMax = chartJson.xMax || 1;
			var format = chartJson.format;
			var chartArea=chartJson.chartArea;
			var xFormat = chartJson.xFormat	|| "{0}";
			var yFormat = chartJson.yFormat	|| "{0}";
			var titleColor = chartJson.titleColor;
			var legendFont = chartJson.legendFont;
			var legendColor = chartJson.legendColor;
			var axisFont = chartJson.axisFont;
			var axisColor = chartJson.axisColor;
			this.oldTitle ={text:chartJson.title};
			var title =   isEmpty(chartJson.title)?null:t(chartJson.x18nName+"."+chartJson.title);
			title = isEmpty(title)?chartJson.title:title;
			var categoryAxis=chartJson.categoryAxis;
			var visible = isEmpty(chartJson.visible)?true:chartJson.visible ;
			var series = new Array();//jQuery.extend([], chartJson.series);
			if (null == titleColor || undefined == titleColor){
				titleColor="#000000";
			}
			if (null == legendColor || undefined == legendColor){
				legendColor="#000000";
			}
			if (null == axisColor || undefined == axisColor){
				axisColor="#000000";
			}
			if(chartArea==null){
				chartArea={background: ""};
			}
			for(var i = 0;i<chartJson.series.length;i++){
				series.push(jQuery.extend({}, chartJson.series[i]));
				if(isNotEmpt(series[i].name)){
					var name = t(chartJson.x18nName+"."+series[i].name);
					series[i].name = isNotEmpt(name)?name:series[i].name;
				}
				series[i].data = [];
			}
			this.maxDot = chartJson.maxDot+1;
			this.chartJson.height=$(object).height();
			this.chartJson.width=$(object).width();
			$(object).kendoChart({
				title: {
					text: title,
					color:titleColor
				},
				legend: {
					labels: {
						color:legendColor
					},
					position: position,
					offsetY: 10,
					margin: {
						top: -10
					}	
				},
				seriesDefaults: {
					type: "scatterLine",
					scatterLine: {
						width: 2
					},
					markers: {
						visible: true,
						size: 0
					}
				},
				chartArea:chartArea,
				categoryAxis:categoryAxis,
				series:series,
				xAxis: {
					max:xMax,
					labels: {
						format: xFormat,
						color:axisColor,
						font: "10px sans-serif"
					}
				},
				yAxis: {
					type: "number",
					min:yMin,
					max: yMax,
					labels: {
						format: yFormat,
						color:axisColor,
						font: "10px sans-serif"
					}
				},
				tooltip: {
					visible: visible,
					format: format
				},
				transitions: false,
				drag: function(){

				},
				dragEnd: function(){

				},
				zoom: function(){

				}
			});
			this.kendoChart = $(object).data("kendoChart");
			this.fillChartData(chartJson.series);
		},
		fillChartData:function(dataJson){						//填充数据
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			var xmax = this.kendoChart.options.xAxis.max;
			var ymax = this.kendoChart.options.yAxis.max;
			if(dataJson.constructor == Object){
				for(var names in dataJson){
					var obj = this.analysisData(names, dataJson[names],serMap,xmax,ymax);
					xmax = obj.xMax > xmax ? obj.xMax < 10? obj.xMax + 1 : obj.xMax *1.1 : xmax;
					ymax = obj.yMax > ymax ? obj.yMax < 10? obj.yMax + 1 : obj.yMax *1.1 : ymax;
				}
			}else if(dataJson.constructor == Array){
				for(var i = 0;i<dataJson.length;i++){
					var obj = this.analysisData(dataJson[i].cname, dataJson[i].data,serMap,xmax,ymax);
					xmax = obj.xMax > xmax ? obj.xMax < 10? obj.xMax + 1 : obj.xMax *1.1 : xmax;
					ymax = obj.yMax > ymax ? obj.yMax < 10? obj.yMax + 1 : obj.yMax *1.1 : ymax;
				}
			}
			this.kendoChart.options.xAxis.max = xmax ;
			this.kendoChart.options.yAxis.max = ymax ;
			if(serMap.values()[0].data.length>0){
				serMap.values()[0].data.push(serMap.values()[0].data[0]);
			}
			this.kendoChart.setOptions({series:serMap.values(),xAxis:this.kendoChart.options.xAxis,yAxis:this.kendoChart.options.yAxis});
			this.kendoChart.refresh();
			this.chartJson.series = serMap.values();
			if($(this.object).height() <=0){
				$(this.object).height(this.chartJson.height);
				$(this.object).width(this.chartJson.width);
			}
		},
		//更新报表数据行名称
		setDataName:function(dataArray){
			if(isEmpty(dataArray)) return;
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			for(var i =0;i<dataArray.length;i++){
				var cname = dataArray[i].cname;
				var seriesData = serMap.get(cname);
				seriesData.name =dataArray[i].newNames + t(this.chartJson.x18nName +"." + dataArray[i].name);
				seriesData.newNames = dataArray[i].newNames;
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
		},
		//解析数据
		analysisData:function(names,array,serMap,xmax,ymax){
			var seriesData = serMap.get(names);
			var arrayDate = seriesData.data.concat(array);
			var maxDot = seriesData.maxDot;
			if(isEmpty(maxDot)){
				maxDot = this.maxDot;
			}
			if(arrayDate.length > maxDot){
				arrayDate.splice(0,arrayDate.length - maxDot);
			}
			seriesData.data = arrayDate;
			for(var i = 0;i<seriesData.data.length;i++){
				var serxMax =parseFloat(seriesData.data[i][0]);
				var seryMax = parseFloat(seriesData.data[i][1]);
				if(xmax < serxMax){
					xmax = serxMax;
				}
				if(ymax < seryMax){
					ymax = seryMax;
				}
			}
			serMap.remove(names);
			serMap.put(names, seriesData);
			return {xMax:xmax,yMax:ymax};
		},
		//国际化kendoChart报表
		x18nKendoChart:function(){
			var newTitle =  jQuery.extend({}, this.oldTitle);
			var oldseries = this.chartJson.series;
			if(isNotEmpt(newTitle.text)){
				newTitle.text = this.combinationTitle + t(this.chartJson.x18nName +"."+newTitle.text);
			}
			var newseries = this.kendoChart.options.series;
			for(var i = 0 ;i < newseries.length;i++){
				if(isNotEmpt(oldseries[i]["name"])){
					var x18nName = t(this.chartJson.x18nName +"."+oldseries[i]["name"]);
					newseries[i]["name"] = isEmpty(newseries[i]["newNames"]) ? x18nName : newseries[i]["newNames" + x18nName];
				}
			}
			this.kendoChart.setOptions({title:newTitle,series:newseries});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表标题名字
		setTitle:function(titles){
			this.combinationTitle = titles;
			var newTitle =  jQuery.extend({}, this.oldTitle);
			newTitle.text = titles + t(this.chartJson.x18nName +"."+newTitle.text);
			this.kendoChart.options.title.text = newTitle.text;
			this.kendoChart.setOptions({title:this.kendoChart.options.title});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//把报表数据源置空
		setNullDataSource:function(){
			$.each(this.kendoChart.options.series,function(){
				this.data=[];
			});
			this.kendoChart.refresh();
		}
};
//柱状报表
var sysColumnChart = function(object,chartJson){
	this.chartJson = chartJson;
	this.object = object;
	this.kendoChart;
	//this.maxDot = 50;  //最多保留多少个点
	this.init(object,chartJson);
	return this;
};

sysColumnChart.prototype={
		init:function(object,chartJson){
			if(isEmpty(object) || isEmpty(chartJson)) return;
			var position = chartJson.position || "top";
			var series = new Array();
			var yMax = chartJson.yMax;
			var yMin = chartJson.yMin;
			var xMax = chartJson.xMax;
			var format = chartJson.format;
			var yFormat = chartJson.yFormat	|| "{0}";
			var titleColor = chartJson.titleColor;
			var legendFont = chartJson.legendFont;
			var legendColor = chartJson.legendColor;
			var axisFont = chartJson.axisFont;
			var axisColor = chartJson.axisColor;
			var chartArea=chartJson.chartArea;
			this.oldTitle ={text:chartJson.title};
			var title =   isEmpty(chartJson.title)?null:t(chartJson.x18nName+"."+chartJson.title);
			var categoryAxis=chartJson.categoryAxis;
			var visible = isEmpty(chartJson.visible)?true:chartJson.visible ;
			if(chartArea==null){
				chartArea={background: ""};
			}
			for(var i = 0;i<chartJson.series.length;i++){
				series.push(jQuery.extend({}, chartJson.series[i]));
				if(isNotEmpt(series[i].name)){
					series[i].name = t(chartJson.x18nName+"."+series[i].name);
				}
				//series[i].data = [];
			}
			//var this.maxDot = chartJson.maxDot;
			$(object).kendoChart({
				title: {
					text: title,
					color:titleColor
				},
				legend: {
					labels: {
						color:legendColor
					},
					position: position,
					offsetY: 10,
					margin: {
						top: -10
					}	
				},
				seriesDefaults: {
					type: "column",
					scatterLine: {
						width: 2
					},
					markers: {
						visible: true,
						size: 0
					}
				},
				chartArea:chartArea,
				categoryAxis:categoryAxis,
				series:series,
				xAxis: {
					max:xMax,
					labels: {
						color:axisColor,
						font: "10px sans-serif"
					}
				},
				yAxis: {
					type: "number",
					min:yMin,
					max: yMax,
					labels: {
						color:axisColor,
						font: "10px sans-serif"
					}
				},
				valueAxis: {
					labels: {
						format:yFormat,
					},
				},
				tooltip: {
					visible: visible,
					format: format
				},
				transitions: false
			});
			this.kendoChart = $(object).data("kendoChart");
		},
		fillChartData:function(dataJson){						//填充数据
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			if(dataJson.constructor == Object){
				for(var names in dataJson){
					var seriesData = serMap.get(names);
					var arrayDate = seriesData.data.concat(dataJson[names]);
//					var maxDot = seriesData.maxDot;
//					if(isEmpty(maxDot)){
//					maxDot = this.maxDot;
//					}
//					if(arrayDate.length > maxDot){
//					arrayDate.splice(0,arrayDate.length - maxDot);
//					}
					seriesData.data = arrayDate;

					serMap.remove(names);
					serMap.put(names, seriesData);
				}
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
			this.chartJson.series = serMap.values();
		},
		//国际化kendoChart报表
		x18nKendoChart:function(){
			var newTitle =  jQuery.extend({}, this.oldTitle);
			var oldseries = this.chartJson.series;
			if(isNotEmpt(newTitle.text)){
				newTitle.text = this.combinationTitle + t(this.chartJson.x18nName +"."+newTitle.text);
			}
			var newseries = this.kendoChart.options.series;
			for(var i = 0 ;i < newseries.length;i++){
				if(isNotEmpt(oldseries[i]["name"])){
					var x18nName = t(this.chartJson.x18nName +"."+oldseries[i]["name"]);
					newseries[i]["name"] = isEmpty(newseries[i]["newNames"]) ? x18nName : newseries[i]["newNames" + x18nName];
				}
			}
			this.kendoChart.setOptions({title:newTitle,series:newseries});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表标题名字
		setTitle:function(titles){
			if(isEmpty(this.oldTitle)) this.oldTitle = this.kendoChart.options.title;
			var newTitle =  jQuery.extend({}, this.oldTitle);
			newTitle.text = titles + newTitle.text;
			this.kendoChart.setOptions({title:newTitle});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表数据行名称
		setDataName:function(dataArray){
			if(isEmpty(dataArray)) return;
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			for(var i =0;i<dataArray.length;i++){
				var cname = dataArray[i].cname;
				var seriesData = serMap.get(cname);
				seriesData.name =dataArray[i].newNames + t(this.chartJson.x18nName +"." + dataArray[i].name);
				seriesData.newNames = dataArray[i].newNames;
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
		},
		//把报表数据源置空
		setNullDataSource:function(){
			$.each(this.kendoChart.options.series,function(){
				this.data=[];
			});
			this.kendoChart.refresh();
		}
};
//可拖动柱状报表
var sysDRAGColumnChart = function(object,chartJson){
	this.chartJson = chartJson;
	this.object = object;
	this.kendoChart;
	this.init(object,chartJson);
	return this;
};

sysDRAGColumnChart.prototype={
		init:function(object,chartJson){
			if(isEmpty(object) || isEmpty(chartJson)) return;
			var position = chartJson.position || "top";
			var yFormat = chartJson.yFormat	|| "{0}";
			var chartArea=chartJson.chartArea;
			if(chartArea==null){
				chartArea={background: ""};
			}
			var datas=chartJson.data;//数据必须是连续的，如果不连续需要做处理
			this.oldTitle ={text:chartJson.title};
			var title =   isEmpty(chartJson.title)?null:t(chartJson.x18nName+"."+chartJson.title);
			var filterField=chartJson.filterField;//X轴下标属性名
			var series=new Array;
			for(var i = 0;i<chartJson.series.length;i++){
				series.push(jQuery.extend({}, chartJson.series[i]));
				if(isNotEmpt(series[i].name)){
					series[i].name = t(chartJson.x18nName+"."+series[i].name);
				}
				//series[i].data = [];
			}
			// Minimum distance in px to start dragging
			var DRAG_STEP =chartJson.dragStep;//每个柱子多少个像素大小

			// Minimum/maximum range length
			var MIN_RANGE = chartJson.minRange;//最小显示柱子数
			var MAX_RANGE = chartJson.maxRange;//最大显示柱子数
			var newStart;
			// Selected Range
			var rangeStart = new Date(chartJson.firstAxis.getTime());//最左边显示的下标
			var start = new Date(chartJson.firstAxis.getTime());//起始显示最左边下标
			var maxttt=new Date(chartJson.lastAxis.getTime());//最大下标
			var rangeLength = MIN_RANGE;//显示的柱子个数
			var tar=this;
			var rangeEnd=new Date(rangeStart.getTime());
			rangeEnd=new Date(rangeEnd.setDate(rangeEnd.getDate()+rangeLength));//最右边显示的下标
			$(object).kendoChart({
				dataSource: {
					data: datas,
					filter:tar.getFilter(filterField,rangeStart, rangeEnd) 
				},
				title: {
					text: title
				},
				legend: {
					position: position,
					offsetY: 10,
					margin: {
						top: -10
					}	
				},
				chartArea:chartArea,
				valueAxis: {
					labels: {
						format:yFormat,
					},
				},
				series: series,
				tooltip: {
					visible: true,
					format: "{0}",
					template: "#= value #"
				},
				transitions: false,
				drag: function (e) {

					var chart = e.sender;
					var ds = chart.dataSource;
					var delta = Math.round(e.originalEvent.x.initialDelta / DRAG_STEP);
					var maxTemp=new Date(maxttt.getTime());
					if (delta != 0) {
						var tmpttt=new Date(rangeStart.getTime());
						newStart = new Date(Math.max(start.getTime(), tmpttt.setDate(tmpttt.getDate()-delta)));
						newStart = new Date(Math.min(maxTemp.setDate(maxTemp.getDate()-rangeLength), newStart.getTime()));
						var tempS=new Date(newStart.getTime());
						ds.filter(tar.getFilter(filterField,newStart, new Date(tempS.setDate(newStart.getDate()+rangeLength))));
					}
				},
				dragEnd:  function (e) {
					rangeStart = newStart;
				},
				zoom: function (e) {
					var chart = e.sender;
					var ds = chart.dataSource;
					//rangeLength = new Date(Math.min(Math.max(tm.setDate(tm.getDate()+ e.delta) , (new Date()).setDate((new Date()).getDate()+ 10)), (new Date()).setDate((new Date()).getDate()+ 20)));
					rangeLength = Math.min(Math.max(rangeLength + e.delta, MIN_RANGE), MAX_RANGE);
					var tempS=new Date(rangeStart.getTime());
					ds.filter(tar.getFilter(filterField,rangeStart, new Date(tempS.setDate(tempS.getDate()+rangeLength))));
					e.originalEvent.preventDefault();
				}
			});
			this.kendoChart = $(object).data("kendoChart");
		},
		getFilter:function(filterField,rangeStart, rangeEnd){
			return [{
				field: filterField,
				operator: "gte",
				value: rangeStart
			}, {
				field: filterField,
				operator: "lte",
				value: rangeEnd
			}];
		},
		fillChartData:function(dataJson){						//填充数据
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			if(dataJson.constructor == Object){
				for(var names in dataJson){
					var seriesData = serMap.get(names);
					var arrayDate = seriesData.data.concat(dataJson[names]);
//					var maxDot = seriesData.maxDot;
//					if(isEmpty(maxDot)){
//					maxDot = this.maxDot;
//					}
//					if(arrayDate.length > maxDot){
//					arrayDate.splice(0,arrayDate.length - maxDot);
//					}
					seriesData.data = arrayDate;

					serMap.remove(names);
					serMap.put(names, seriesData);
				}
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
			this.chartJson.series = serMap.values();
		},
		//国际化kendoChart报表
		x18nKendoChart:function(){
			var newTitle =  jQuery.extend({}, this.oldTitle);
			var oldseries = this.chartJson.series;
			if(isNotEmpt(newTitle.text)){
				newTitle.text = this.combinationTitle + t(this.chartJson.x18nName +"."+newTitle.text);
			}
			var newseries = this.kendoChart.options.series;
			for(var i = 0 ;i < newseries.length;i++){
				if(isNotEmpt(oldseries[i]["name"])){
					var x18nName = t(this.chartJson.x18nName +"."+oldseries[i]["name"]);
					newseries[i]["name"] = isEmpty(newseries[i]["newNames"]) ? x18nName : newseries[i]["newNames" + x18nName];
				}
			}
			this.kendoChart.setOptions({title:newTitle,series:newseries});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表标题名字
		setTitle:function(titles){
			if(isEmpty(this.oldTitle)) this.oldTitle = this.kendoChart.options.title;
			var newTitle =  jQuery.extend({}, this.oldTitle);
			newTitle.text = titles + newTitle.text;
			this.kendoChart.setOptions({title:newTitle});
			this.kendoChart.refresh();
			this.chartJson.title = newTitle.text;
		},
		//更新报表数据行名称
		setDataName:function(dataArray){
			if(isEmpty(dataArray)) return;
			var seriesArray = this.kendoChart.options.series;
			var serMap = new Map();
			for(var i =0;i<seriesArray.length;i++){
				serMap.put(seriesArray[i].cname, seriesArray[i]);
			}
			for(var i =0;i<dataArray.length;i++){
				var cname = dataArray[i].cname;
				var seriesData = serMap.get(cname);
				seriesData.name =dataArray[i].newNames + t(this.chartJson.x18nName +"." + dataArray[i].name);
				seriesData.newNames = dataArray[i].newNames;
			}
			this.kendoChart.setOptions({series:serMap.values()});
			this.kendoChart.refresh();
		},
		//把报表数据源置空
		setNullDataSource:function(){
			$.each(this.kendoChart.options.series,function(){
				this.data=[];
			});
			this.kendoChart.refresh();
		}
};

//仪表盘对象
var sysGauge = function(object,gaugeJson){
	this.chartJson = chartJson;
	this.object = object;
	this.kendoGauge;
	this.init(object,gaugeJson);
	return this;
};

sysGauge.prototype={
		init:function(object,gaugeJson){
			var val = gaugeJson.val || 0;
			var startAngle =   gaugeJson.startAngle;
			var endAngle =   gaugeJson.endAngle;
			var max = gaugeJson.max;
			var minorUnit = gaugeJson.minorUnit || 5;
			var ranges = gaugeJson.ranges;
			$(object).kendoRadialGauge({
				pointer: {
					value: val
				},
				scale: {
					minorUnit: minorUnit,
					startAngle: startAngle,
					endAngle: endAngle,
					max: max,
					labels: {
						position: "inside"
					},
					ranges:ranges
				}
			});
			this.kendoGauge = $(object).data("kendoRadialGauge");
		},
		fillChartData:function(values){
			this.kendoGauge.value(values);
		},
		//国际化kendoChart报表
		x18nKendoChart:function(){

		},
		//更新报表标题名字
		setTitle:function(titles){
			this.kendoChart.setOptions({title:titles});
		}
};